package com.huaruan.youdi.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.eventbus.EventBus;
import com.huaruan.youdi.eventbus.EventNames;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpUtils;
import com.huaruan.youdi.models.OrderModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.activitys.AppealActivity;
import com.huaruan.youdi.views.activitys.BaseActivity;
import com.huaruan.youdi.views.dialogs.BaseDialog;
import com.huaruan.youdi.views.dialogs.SelectMapDialog;
import com.huaruan.youdi.views.widget.easyrecyclerview.adapter.BaseViewHolder;
import com.huaruan.youdi.views.widget.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.SortedMap;
import java.util.TreeMap;


public class OrderAdapter extends RecyclerArrayAdapter<OrderModel> {
    Context context;
    String type;

    public OrderAdapter(Context context,String type) {
        super(context);
        this.context = context;
        this.type = type;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    class ViewHolder extends BaseViewHolder<OrderModel> {
        TextView tv_price;
        TextView tv_date;
        TextView tv_send_name;
        TextView tv_send_phone;
        TextView tv_send_address;
        TextView tv_receive_name;
        TextView tv_receive_phone;
        TextView tv_receive_address;
        View view_bottom;
        LinearLayout ll_bottom;
        TextView tv_contact;
        TextView tv_channel;
        TextView tv_go;
        TextView tv_confirm;

        public ViewHolder(ViewGroup itemView) {
            super(itemView, R.layout.item_order);
            this.tv_price = $(R.id.tv_price);
            this.tv_date = $(R.id.tv_date);
            this.tv_send_name = $(R.id.tv_send_name);
            this.tv_send_phone = $(R.id.tv_send_phone);
            this.tv_send_address = $(R.id.tv_send_address);
            this.tv_receive_name = $(R.id.tv_receive_name);
            this.tv_receive_phone = $(R.id.tv_receive_phone);
            this.tv_receive_address = $(R.id.tv_receive_address);
            this.view_bottom = $(R.id.view_bottom);
            this.ll_bottom = $(R.id.ll_bottom);
            this.tv_contact = $(R.id.tv_contact);
            this.tv_channel = $(R.id.tv_channel);
            this.tv_go = $(R.id.tv_go);
            this.tv_confirm = $(R.id.tv_confirm);
        }

        @Override
        public void setData(final OrderModel model) {
            super.setData(model);
            tv_price.setText(model.amount);
            tv_date.setText(model.createTime);
            tv_send_name.setText(model.sender);
            tv_send_phone.setText(model.senderPhone);
            tv_send_address.setText(model.senderAddress);
            tv_receive_name.setText(model.receiver);
            tv_receive_phone.setText(model.receiverPhone);
            tv_receive_address.setText(model.receiverAddress);

            if("6".equals(type)){
                view_bottom.setVisibility(View.GONE);
                ll_bottom.setVisibility(View.GONE);
            }
            final String phone = model.receiverPhone;
            tv_contact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + phone));
                    context.startActivity(intent);
                }
            });
            tv_channel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("waybillId",model.wbId);
                    AppUtils.jump(context, AppealActivity.class,bundle);
                }
            });
            tv_go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectMapDialog selectMapDialog = new SelectMapDialog();
                    Bundle bundle = new Bundle();
                    bundle.putString("address",model.receiverAddress);
                    selectMapDialog.setArguments(bundle);
                    selectMapDialog.show(((BaseActivity) context).getSupportFragmentManager(), "");
                }
            });
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final BaseDialog dialog = new BaseDialog(context);
                    dialog.setContent("确定商品已送达吗")
                            .setLeftButtonText("取消")
                            .setRightButtonText("确定")
                            .setLeftButtonListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            })
                            .setRightButtonListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    signFor(model.wbId);
                                }
                            });
                    dialog.show();
                }
            });
        }

        private void signFor(String wbId) {
            SortedMap<String, String> sortMap = new TreeMap<>();
            sortMap.put("userId", UserUtils.getUid());
            sortMap.put("wbId", wbId);
            new HttpUtils.Builder(context)
                    .url(Constant.CONFIRM_ORDER)
                    .params(sortMap)
                    .callback(new HttpCallback() {
                        @Override
                        public void success(Object object) {
                            AppUtils.toastShort(context, "确认送达");
                            EventBus.getInstance().notifiDataUpdate(EventNames.REFRESH_MAIN, "");
                        }
                    }).start();
        }
    }
}
